package com.bigdata.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bigdata.pojo.*;

import java.util.Random;

/**
 * @className: GenerateFieldsDataUtil
 * @description: TODO 属性数据生成工具类
 * @author 石永鑫大数据工作室
 * @date: 2021/3/12 21:46
 * @version: 1.0
 */
public class GenerateFieldsDataUtil {

    private static Random rand = new Random();
    // 设备id
    private static int s_mid = 0;
    // 用户id
    private static int s_uid = 0;
    // 商品id
    private static int s_goodsid = 0;

    /**
     * 生成APP启动日志数据：
     */
    public static AppStart generateStart() {

        AppStart appStart = new AppStart();
        //1、设备id
        appStart.setMid(s_mid + "");
        s_mid++;
        //2、用户id
        appStart.setUid(s_uid + "");
        s_uid++;
        //3、程序版本号 5,6等
        appStart.setVc("" + rand.nextInt(20));
        //4、程序版本名 v1.1.1
        appStart.setVn("1." + rand.nextInt(4) + "." + rand.nextInt(10));
        //5、安卓系统版本
        appStart.setOs("8." + rand.nextInt(3) + "." + rand.nextInt(10));
        //6、设置日志类型
        appStart.setEn("start");
        //7、语言  es,en,pt
        int flag = rand.nextInt(3);
        switch (flag) {
            case (0):
                appStart.setL("es");
                break;
            case (1):
                appStart.setL("en");
                break;
            case (2):
                appStart.setL("pt");
                break;
        }
        //8、渠道号   从哪个渠道来的
        appStart.setSr(DataUtil.getRandomChar(1));
        //9、区域
        flag = rand.nextInt(2);
        switch (flag) {
            case 0:
                appStart.setAr("BR");
            case 1:
                appStart.setAr("MX");
        }
        //9、手机品牌 ba ,手机型号 md，就取2位数字了
        flag = rand.nextInt(3);
        switch (flag) {
            case 0:
                appStart.setBa("Sumsung");
                appStart.setMd("sumsung-" + rand.nextInt(20));
                break;
            case 1:
                appStart.setBa("Huawei");
                appStart.setMd("Huawei-" + rand.nextInt(20));
                break;
            case 2:
                appStart.setBa("HTC");
                appStart.setMd("HTC-" + rand.nextInt(20));
                break;
        }
        //10、嵌入sdk的版本
        appStart.setSv("V2." + rand.nextInt(10) + "." + rand.nextInt(10));
        //11、gmail
        appStart.setG(DataUtil.getRandomCharAndNumr(8) + "@gmail.com");
        //12、屏幕宽高 hw
        flag = rand.nextInt(4);
        switch (flag) {
            case 0:
                appStart.setHw("640*960");
                break;
            case 1:
                appStart.setHw("640*1136");
                break;
            case 2:
                appStart.setHw("750*1134");
                break;
            case 3:
                appStart.setHw("1080*1920");
                break;
        }
        //13、客户端产生日志时间
        long millis = System.currentTimeMillis();
        appStart.setT("" + (millis - rand.nextInt(99999999)));
        //14、手机网络模式 3G,4G,WIFI
        flag = rand.nextInt(3);
        switch (flag) {
            case 0:
                appStart.setNw("3G");
                break;
            case 1:
                appStart.setNw("4G");
                break;
            case 2:
                appStart.setNw("WIFI");
                break;
        }
        // 拉丁美洲 西经34°46′至西经117°09；北纬32°42′至南纬53°54′
        //15、经度
        appStart.setLn((-34 - rand.nextInt(83) - rand.nextInt(60) / 10.0) + "");
        //16、纬度
        appStart.setLa((32 - rand.nextInt(85) - rand.nextInt(60) / 10.0) + "");
        //17、入口
        flag = rand.nextInt(5) + 1;
        appStart.setEntry(flag + "");
        //18、开屏广告类型
        flag = rand.nextInt(2) + 1;
        appStart.setOpen_ad_type(flag + "");
        //19、状态
        flag = rand.nextInt(10) > 8 ? 2 : 1;
        appStart.setAction(flag + "");
        //20、加载时长
        appStart.setLoading_time(rand.nextInt(20) + "");
        //21、失败码
        flag = rand.nextInt(10);
        switch (flag) {
            case 1:
                appStart.setDetail("102");
                break;
            case 2:
                appStart.setDetail("201");
                break;
            case 3:
                appStart.setDetail("325");
                break;
            case 4:
                appStart.setDetail("433");
                break;
            case 5:
                appStart.setDetail("542");
                break;
            default:
                appStart.setDetail("");
                break;
        }
        //22、扩展字段
        appStart.setExtend1("");

        return appStart;
    }


    /**
     * 公共字段数据
     * @return
     */
    public static JSONObject generateComFields() {

        AppBase appBase = new AppBase();
        //1、设备id
        appBase.setMid(s_mid + "");
        s_mid++;
        //2、用户id
        appBase.setUid(s_uid + "");
        s_uid++;
        //3、程序版本号 5,6等
        appBase.setVc("" + rand.nextInt(20));
        //4、程序版本名 v1.1.1
        appBase.setVn("1." + rand.nextInt(4) + "." + rand.nextInt(10));
        //5、安卓系统版本
        appBase.setOs("8." + rand.nextInt(3) + "." + rand.nextInt(10));
        //6、语言  es,en,pt
        int flag = rand.nextInt(3);
        switch (flag) {
            case (0):
                appBase.setL("es");
                break;
            case (1):
                appBase.setL("en");
                break;
            case (2):
                appBase.setL("pt");
                break;
        }
        //7、渠道号   从哪个渠道来的
        appBase.setSr(DataUtil.getRandomChar(1));
        //8、区域
        flag = rand.nextInt(2);
        switch (flag) {
            case 0:
                appBase.setAr("BR");
            case 1:
                appBase.setAr("MX");
        }
        //9、手机品牌 ba ,手机型号 md，就取2位数字了
        flag = rand.nextInt(3);
        switch (flag) {
            case 0:
                appBase.setBa("Sumsung");
                appBase.setMd("sumsung-" + rand.nextInt(20));
                break;
            case 1:
                appBase.setBa("Huawei");
                appBase.setMd("Huawei-" + rand.nextInt(20));
                break;
            case 2:
                appBase.setBa("HTC");
                appBase.setMd("HTC-" + rand.nextInt(20));
                break;
        }
        //10、嵌入sdk的版本
        appBase.setSv("V2." + rand.nextInt(10) + "." + rand.nextInt(10));
        //11、gmail
        appBase.setG(DataUtil.getRandomCharAndNumr(8) + "@gmail.com");
        //12、屏幕宽高 hw
        flag = rand.nextInt(4);
        switch (flag) {
            case 0:
                appBase.setHw("640*960");
                break;
            case 1:
                appBase.setHw("640*1136");
                break;
            case 2:
                appBase.setHw("750*1134");
                break;
            case 3:
                appBase.setHw("1080*1920");
                break;
        }
        //13、客户端产生日志时间
        long millis = System.currentTimeMillis();
        appBase.setT("" + (millis - rand.nextInt(99999999)));
        //14、手机网络模式 3G,4G,WIFI
        flag = rand.nextInt(3);
        switch (flag) {
            case 0:
                appBase.setNw("3G");
                break;
            case 1:
                appBase.setNw("4G");
                break;
            case 2:
                appBase.setNw("WIFI");
                break;
        }
        // 拉丁美洲 西经34°46′至西经117°09；北纬32°42′至南纬53°54′
        //14、经度
        appBase.setLn((-34 - rand.nextInt(83) - rand.nextInt(60) / 10.0) + "");
        //15、纬度
        appBase.setLa((32 - rand.nextInt(85) - rand.nextInt(60) / 10.0) + "");

        return (JSONObject) JSON.toJSON(appBase);
    }


    /**
     * 商品点击事件字段数据
     * @return 商品点击事件字段JSON对象
     */
    public static JSONObject generateDisplay() {

        AppDisplay appDisplay = new AppDisplay();

        boolean boolFlag = rand.nextInt(10) < 7;
        //1、动作：曝光商品=1，点击商品=2，
        if (boolFlag) {
            appDisplay.setAction("1");
        } else {
            appDisplay.setAction("2");
        }
        //2、商品id
        String goodsId = s_goodsid + "";
        s_goodsid++;
        appDisplay.setGoodsid(goodsId);
        //3、顺序  设置成6条
        int flag = rand.nextInt(6);
        appDisplay.setPlace("" + flag);
        //4、曝光类型
        flag = 1 + rand.nextInt(2);
        appDisplay.setExtend1("" + flag);
        //5、分类
        flag = 1 + rand.nextInt(100);
        appDisplay.setCategory("" + flag);

        JSONObject jsonObject = (JSONObject) JSON.toJSON(appDisplay);

        return DataUtil.packEventJson("display", jsonObject);
    }


    /**
     * 商品详情页事件字段数据
     * @return
     */
    public static JSONObject generateNewsDetail() {

        AppNewsDetail appNewsDetail = new AppNewsDetail();

        //1、页面入口来源
        int flag = 1 + rand.nextInt(3);
        appNewsDetail.setEntry(flag + "");
        //2、动作
        appNewsDetail.setAction("" + (rand.nextInt(4) + 1));
        //3、商品id
        appNewsDetail.setGoodsid(s_goodsid + "");
        //4、商品来源类型
        flag = 1 + rand.nextInt(3);
        appNewsDetail.setShowtype(flag + "");
        //5、商品样式
        flag = rand.nextInt(6);
        appNewsDetail.setShowtype("" + flag);
        //6、页面停留时长
        flag = rand.nextInt(10) * rand.nextInt(7);
        appNewsDetail.setNews_staytime(flag + "");
        //7、加载时长
        flag = rand.nextInt(10) * rand.nextInt(7);
        appNewsDetail.setLoading_time(flag + "");
        //8、加载失败码
        flag = rand.nextInt(10);
        switch (flag) {
            case 1:
                appNewsDetail.setType1("102");
                break;
            case 2:
                appNewsDetail.setType1("201");
                break;
            case 3:
                appNewsDetail.setType1("325");
                break;
            case 4:
                appNewsDetail.setType1("433");
                break;
            case 5:
                appNewsDetail.setType1("542");
                break;
            default:
                appNewsDetail.setType1("");
                break;
        }
        //9、分类
        flag = 1 + rand.nextInt(100);
        appNewsDetail.setCategory("" + flag);

        JSONObject eventJson = (JSONObject) JSON.toJSON(appNewsDetail);
        return DataUtil.packEventJson("newsdetail", eventJson);
    }


    /**
     * 商品列表页事件字段数据
     * @return
     */
    public static JSONObject generateNewList() {

        AppLoading appLoading = new AppLoading();
        //1、动作
        int flag = rand.nextInt(3) + 1;
        appLoading.setAction(flag + "");
        //2、加载时长
        flag = rand.nextInt(10) * rand.nextInt(7);
        appLoading.setLoading_time(flag + "");
        //3、失败码
        flag = rand.nextInt(10);
        switch (flag) {
            case 1:
                appLoading.setType1("102");
                break;
            case 2:
                appLoading.setType1("201");
                break;
            case 3:
                appLoading.setType1("325");
                break;
            case 4:
                appLoading.setType1("433");
                break;
            case 5:
                appLoading.setType1("542");
                break;
            default:
                appLoading.setType1("");
                break;
        }
        //4、页面  加载类型
        flag = 1 + rand.nextInt(2);
        appLoading.setLoading_way("" + flag);
        //5、扩展字段1
        appLoading.setExtend1("");
        //6、扩展字段2
        appLoading.setExtend2("");
        //7、用户加载类型
        flag = 1 + rand.nextInt(3);
        appLoading.setType("" + flag);

        JSONObject jsonObject = (JSONObject) JSON.toJSON(appLoading);
        return DataUtil.packEventJson("loading", jsonObject);
    }


    /**
     * 广告相关字段
     */
    public static JSONObject generateAd() {

        AppAd appAd = new AppAd();
        //1、入口
        int flag = rand.nextInt(3) + 1;
        appAd.setEntry(flag + "");
        //2、动作
        flag = rand.nextInt(5) + 1;
        appAd.setAction(flag + "");
        //3、状态
        flag = rand.nextInt(10) > 6 ? 2 : 1;
        appAd.setContent(flag + "");
        //4、失败码
        flag = rand.nextInt(10);
        switch (flag) {
            case 1:
                appAd.setDetail("102");
                break;
            case 2:
                appAd.setDetail("201");
                break;
            case 3:
                appAd.setDetail("325");
                break;
            case 4:
                appAd.setDetail("433");
                break;
            case 5:
                appAd.setDetail("542");
                break;
            default:
                appAd.setDetail("");
                break;
        }
        //5、广告来源
        flag = rand.nextInt(4) + 1;
        appAd.setSource(flag + "");
        //6、用户行为
        flag = rand.nextInt(2) + 1;
        appAd.setBehavior(flag + "");
        //7、商品类型
        flag = rand.nextInt(10);
        appAd.setNewstype("" + flag);
        //8、展示样式
        flag = rand.nextInt(6);
        appAd.setShow_style("" + flag);

        JSONObject jsonObject = (JSONObject) JSON.toJSON(appAd);
        return DataUtil.packEventJson("ad", jsonObject);
    }


    /**
     * 消息通知
     */
    public static JSONObject generateNotification() {

        AppNotification appNotification = new AppNotification();
        int flag = rand.nextInt(4) + 1;
        //1、动作
        appNotification.setAction(flag + "");
        //2、通知id
        flag = rand.nextInt(4) + 1;
        appNotification.setType(flag + "");
        //3、客户端弹时间
        appNotification.setAp_time((System.currentTimeMillis() - rand.nextInt(99999999)) + "");
        //4、备用字段
        appNotification.setContent("");

        JSONObject jsonObject = (JSONObject) JSON.toJSON(appNotification);
        return DataUtil.packEventJson("notification", jsonObject);
    }

    /**
     * 后台活跃
     */
    public static JSONObject generateBackground() {
        AppActive_background appActive_background = new AppActive_background();
        // 启动源
        int flag = rand.nextInt(3) + 1;
        appActive_background.setActive_source(flag + "");
        JSONObject jsonObject = (JSONObject) JSON.toJSON(appActive_background);
        return DataUtil.packEventJson("active_background", jsonObject);
    }


    /**
     * 用户评论
     */
    public static JSONObject generateComment() {

        AppComment comment = new AppComment();
        comment.setComment_id(rand.nextInt(10));
        comment.setUserid(rand.nextInt(10));
        comment.setP_comment_id(rand.nextInt(5));
        comment.setContent(DataUtil.getCONTENT());
        comment.setAddtime((System.currentTimeMillis() - rand.nextInt(99999999)) + "");
        comment.setOther_id(rand.nextInt(10));
        comment.setPraise_count(rand.nextInt(1000));
        comment.setReply_count(rand.nextInt(200));

        JSONObject jsonObject = (JSONObject) JSON.toJSON(comment);
        return DataUtil.packEventJson("comment", jsonObject);
    }


    /**
     * 收藏
     */
    public static JSONObject generateFavorites() {

        AppFavorites favorites = new AppFavorites();
        favorites.setCourse_id(rand.nextInt(10));
        favorites.setUserid(rand.nextInt(10));
        favorites.setAdd_time((System.currentTimeMillis() - rand.nextInt(99999999)) + "");

        JSONObject jsonObject = (JSONObject) JSON.toJSON(favorites);
        return DataUtil.packEventJson("favorites", jsonObject);
    }


    /**
     * 点赞
     */
    public static JSONObject generatePraise() {

        AppPraise praise = new AppPraise();
        praise.setId(rand.nextInt(10));
        praise.setUserid(rand.nextInt(10));
        praise.setTarget_id(rand.nextInt(10));
        praise.setType(rand.nextInt(4) + 1);
        praise.setAdd_time((System.currentTimeMillis() - rand.nextInt(99999999)) + "");

        JSONObject jsonObject = (JSONObject) JSON.toJSON(praise);
        return DataUtil.packEventJson("praise", jsonObject);
    }



    /**
     * 错误日志数据
     */
    public static JSONObject generateError() {

        AppErrorLog appErrorLog = new AppErrorLog();
        //错误摘要
        String[] errorBriefs = {
                "at cn.lift.dfdf.web.AbstractBaseController.validInbound(AbstractBaseController.java:72)",
                "at cn.lift.appIn.control.CommandUtil.getInfo(CommandUtil.java:67)"
        };
        //错误详情
        String[] errorDetails = {
                "java.lang.NullPointerException\\n    " + "at cn.lift.appIn.web.AbstractBaseController.validInbound(AbstractBaseController.java:72)\\n " + "at cn.lift.dfdf.web.AbstractBaseController.validInbound",
                "at cn.lift.dfdfdf.control.CommandUtil.getInfo(CommandUtil.java:67)\\n " + "at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\\n" + " at java.lang.reflect.Method.invoke(Method.java:606)\\n"
        };
        //错误摘要
        appErrorLog.setErrorBrief(errorBriefs[rand.nextInt(errorBriefs.length)]);
        //错误详情
        appErrorLog.setErrorDetail(errorDetails[rand.nextInt(errorDetails.length)]);

        JSONObject jsonObject = (JSONObject) JSON.toJSON(appErrorLog);
        return DataUtil.packEventJson("error", jsonObject);
    }
}
